package com.mywind.controller.core;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.mywind.constant.Constant;
import com.mywind.entity.core.Function;
import com.mywind.entity.core.ResponseObj;
import com.mywind.entity.core.User;
import com.mywind.service.core.IFunctionServ;
import com.mywind.service.core.IUserServ;

/**
 * @author A01162
 * 
 */
@Controller
@SessionAttributes(value = { "mysession", "lockInfos" })
@RequestMapping(value = "/user")
public class LoginController implements Constant {

	@Autowired
	private IUserServ userServ;
	@Autowired
	private IFunctionServ functionServ;

	@RequestMapping(value = "/toLogin.do")
	public String toLogin() {
		return "login";
	}

	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObj userLogin(@RequestParam("account") String account,
			@RequestParam("password") String password, ModelMap modelMap) {
		ResponseObj rb = new ResponseObj();
		List<User> users = userServ.findUserByAccPwd(account, password);
		User user;
		if (users.size() != 1) {
			rb.setStatus(ERR);
			rb.setMessage("用户名密码不匹配，请重新输入");
			return rb;
		} else {
			user = users.get(0);
			if ("1".equals(user.getStatus())) {
				rb.setStatus(ERR);
				rb.setMessage("该用户已被冻结，请联系管理员");
				return rb;
			}
		}

		modelMap.addAttribute("mysession", user);
		rb.setStatus(SUC);
		rb.setMessage("登录成功");
		return rb;
	}

	// 退出系统LogOut.do
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/logout.do")
	public String logout(HttpServletRequest request, SessionStatus status) {
		// 清除session
		Enumeration<String> em = request.getSession().getAttributeNames();
		while (em.hasMoreElements()) {
			request.getSession().removeAttribute(em.nextElement().toString());
		}
		request.getSession().invalidate();
		status.setComplete();
		return "redirect:/user/toLogin.do";
	}

	@RequestMapping(value = "/main.do")
	public String toMain() {
		return "main";
	}

	/**
	 * 主界面的菜单数据查询
	 * 
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/loadMain.do", method = RequestMethod.GET)
	@ResponseBody
	public ResponseObj loadMain(ModelMap modelMap) {
		ResponseObj rb = new ResponseObj();
		User mysession = (User) modelMap.get("mysession");
		List<Function> parentFuncs = null;
		Map<String, List<Function>> respData = new HashMap<String, List<Function>>();
		Map<String, Object> otherRes = new HashMap<String, Object>();

		List<Integer> parentIds = new ArrayList<Integer>();
		if (mysession != null) {
			List<Function> funcs = userServ
					.findFuncsByUserId(mysession.getId());
			if (funcs != null && funcs.size() != 0) {
				for (int i = 0; i < funcs.size(); i++) {
					parentIds.add(funcs.get(i).getParentId());
				}
				parentFuncs = functionServ.getParentFuncsByIds(parentIds);
				respData.put("parentFuncs", parentFuncs);
				respData.put("funcs", funcs);
				rb.setStatus(SUC);
				rb.setResBody(respData);
				rb.setOtherRes(otherRes);
				return rb;
			} else {
				rb.setStatus(ERR);
				rb.setErrCode(1);
				rb.setMessage("该用户还没有任何菜单使用权限");
				return rb;
			}
		}
		rb.setStatus(ERR);
		rb.setErrCode(2);
		rb.setMessage("查询失败");
		return rb;
	}
}
